/**
 * 
 */
package controller;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alee.laf.optionpane.WebOptionPane;

import database.RecipeDatabase;
import model.Recipe;
import view.AddRecipePanel2;
import view.RecipeDisplayPanel;
import view.RecipeGUI;
import view.RecipeListPanel;
import view.SearchPanel;

/**
 * @author Jamada, Natalie
 *
 */
public class RecipeControls implements Controls {
    private String my_userID;
    private RecipeGUI my_view;
    private RecipeDatabase my_database;
    private List<Recipe> my_current_recipes;
    
    
    public RecipeControls() {
        
        my_database = new RecipeDatabase();
        my_current_recipes = new ArrayList<Recipe>();
        my_view = new RecipeGUI(this);

    }
    /* (non-Javadoc)
     * @see controller.Controls#start()
     */
    @Override
    public void start() {
        my_view.createInitialView();
    }

    /* (non-Javadoc)
     * @see controller.Controls#login(java.lang.String, java.lang.String)
     */
    @Override
    public void login(String userID, String password) {
        //test
        if (my_database.validateUser(userID, password)) {
        	my_userID = userID;
            my_view.createView();
        } else {
            WebOptionPane.showMessageDialog (my_view.getFrame(), "Incorrect username or password.", "Invalid Login", WebOptionPane.WARNING_MESSAGE );
        }

    }
    /* (non-Javadoc)
     * @see controller.Controls#search()
     */
    @Override
    public boolean search(List<String> searchConditions) {
        boolean result = true;
        my_current_recipes.clear();
    	my_current_recipes.addAll(my_database.searchRecipes(searchConditions));
    	if (my_current_recipes.size() == 0) {
    	    result = false;
    	}
    	return result;
    } 
    /* (non-Javadoc)
     * @see controller.Controls#addRecipe() 
     */
    @Override
    public boolean addRecipe(List<String> newRecipe, List<List<String>> ingredients, List<String> steps) {
        return my_database.addRecipe(newRecipe, ingredients, steps);
   
    }

    /* (non-Javadoc)
     * @see controller.Controls#editRecipe()
     */
    @Override
    public Recipe editRecipe(int recipeID, List<String> recipeInformation, List<List<String>> ingredients, List<String> steps) {
        return my_database.editRecipe(recipeID, recipeInformation, ingredients, steps);
    }

    /* (non-Javadoc)
     * @see controller.Controls#displayRecipe()
     */
    @Override
    public void displayRecipe(Recipe the_recipe) {
    	//this will trigger a recently viewed entry 
    	Date the_time = new Date();
    	my_database.addRecentView(my_userID, "" + the_recipe.getID(), new Timestamp(the_time.getTime()));
    	my_database.addRecipeView(the_recipe.getID());
        my_view.setBody(new RecipeDisplayPanel(this, the_recipe));
    }

    /* (non-Javadoc)
     * @see controller.Controls#addFavorite()
     */
    @Override
    public void addFavorite(Recipe favorite) {
    	my_database.addFavorite(my_userID, favorite);

    }
    
	@Override
	public void removeFavorite(Recipe favorite) {
		my_database.removeFavorite(my_userID, favorite.getID());		
	}
    
	@Override
	public void addUser(List<String> userInfo) {
		my_database.addUser(userInfo);
	}
    

    /* (non-Javadoc)
     * @see controller.Controls#goSearch()
     */
    @Override
    public void goSearch() {
        //my_view.setBody(new SearchPanel(this));
        //Test version below, real version above.
        my_view.setBody(new SearchPanel(this));
    }
    /* (non-Javadoc)
     * @see controller.Controls#goFavorites()
     */
    @Override
    public void goFavorites() {
        my_view.setBody(new RecipeListPanel(this, this.getFavorites(), "Favorites"));
    }

    /* (non-Javadoc)
     * @see controller.Controls#goRecentlyViewed()
     */
    @Override
    public void goRecentlyViewed() {
        my_view.setBody(new RecipeListPanel(this, this.getRecentlyViewed(), "Recently Viewed"));
    }

    /* (non-Javadoc)
     * @see controller.Controls#goAddRecipe()
     */
    @Override
    public void goAddRecipe() {
        my_view.setBody(new AddRecipePanel2(this));

    }

    /* (non-Javadoc)
     * @see controller.Controls#logout()
     */
    @Override
    public void logout() {
        my_view.logout();

    }
    @Override
    public List<Recipe> getRecentlyViewed() {
        return my_database.getRecentlyViewedRecipes(my_userID);
    }
    
    @Override
    public List<Recipe> getFavorites() {
        return my_database.getFavoriteRecipes(my_userID);
    }
    @Override
    public List<String> getCuisines() {
        return my_database.getStaticValues("Cuisines", "Cuisine_Name");
    }
    @Override
    public List<String> getCourses() {
        return my_database.getStaticValues("Courses", "Course_Name");
    }
    @Override
    public List<String> getDifficulty() {
        return my_database.getStaticValues("Difficulty_Types", "Difficulty");
    }
    @Override
    public List<String> getFoodGroups() {
        return my_database.getStaticValues("Ingredient_Groups", "Ingredient_Group");
    }
    @Override
	public List<String> getUnitMeasures() {
    	return my_database.getStaticValues("Unit_Measures", "Description");
	}
	@Override
	public Boolean isFavorite(Recipe favorite) {
		return my_database.isFavorite(favorite.getID(), my_userID);
	}
    @Override
    public void goEditRecipe(Recipe the_recipe) {
        my_view.setBody(new AddRecipePanel2(this, the_recipe));
        
    }
    @Override
    public void goSearchResults() {
        my_view.setBody(new RecipeListPanel(this, my_current_recipes, "Search Results"));
        
    }
	



}
